#pragma once

#include <sys/epoll.h>
#include "Log.hpp"
#include "Common.hpp"
using namespace LogModule;
class Epoller
{
public:
    Epoller()
    {
        _epollfd = epoll_create(128);
        if (_epollfd < 0)
        {
            LOG(Level::FATAL) << "epoll_create fail";
            exit(EPOLL_CLOEXEC);
        }
        LOG(Level::INFO)<<"epoll_create success";
    }
    void ModEventHelper(int fd, int oper, uint32_t es)
    {
        epoll_event ev;
        ev.events = es;
        ev.data.fd = fd;
        epoll_ctl(_epollfd,oper,fd,&ev);
    }
    void EpollCtlAdd(int fd, uint32_t es)
    {
        ModEventHelper(fd,EPOLL_CTL_ADD,es);
    }
    void EpollCtlDel(int fd)
    {
        ModEventHelper(fd,EPOLL_CTL_DEL,0);
    }
    void EpollCtlMod(int fd, uint32_t es)
    {
        ModEventHelper(fd,EPOLL_CTL_MOD,es);
    }
    int EpollWait(struct epoll_event revs[], int maxnum, int timeout)
    {
        int n = epoll_wait(_epollfd,revs,maxnum,timeout);
        if(n<0)
        {
            LOG(Level::WARING)<<"epoll_wait error";
        }
        else if(n==0)
        {
            //LOG(Level::INFO)<<"epoll_wait timeout...";
        }
        else
        {
            //TODO
        }
        return n;
    }

private:
    int _epollfd;
};